package com.cloudfun.msglisten.ws.haier.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Huqin on 2019/12/17
 */
@Data
@Component
@ConfigurationProperties(prefix = "haier.ws")
public class HaierWsProperties {

    private String schema = "wss://";

    /**
     * 是否启用
     */
    private boolean enable;

    /**
     * ws服务器地址
     */
    private String host;

    /**
     * ws服务路径
     */
    private String uri;

    /**
     * ws服务客户端id
     */
    private String systemId;

    /**
     * ws服务客户端key
     */
    private String systemKey;

    /**
     * 是否从头订阅
     */
    private Boolean isSubscribeEarliestMessage;

    /**
     * 订阅配置
     */
    private List<SubscribeProperties> subscribes = new ArrayList<>();

    /**
     * 心跳配置
     */
    private HeartBeatProperties heartBeat = new HeartBeatProperties();

    /**
     * 重连配置
     * 重连等待时间为:（乘数^当前次数）*延迟时间
     */
    private ReconnectProperties reconnect = new ReconnectProperties();

    private MsgHandlerPool msgHandlerPool = new MsgHandlerPool();

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SubscribeProperties {

        /**
         * 订阅主题
         */
        private String topic;

        /**
         * typeId集合
         */
        private List<String> typeIds;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class HeartBeatProperties {
        /**
         * 任务初始延迟
         */
        private long initDelay = 10;

        /**
         * 心跳间隔
         */
        private long interval = 50;

        /**
         * 任务周期，建议为心跳间隔/2
         */
        private long period = interval / 2;

        /**
         * 10 min
         */
        private long heartBeatMaxIntervalTime = 10 * 60;

        /**
         * 5min内统计心跳次数超过3次后，需要发送警告
         */
        private long maxHeatBeatCountDuringInterval = 3;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ReconnectProperties {
        /**
         * 乘数
         */
        private int multiplier = 2;

        /**
         * 最大重试次数
         */
        private int maxRetryTimes = 8;

        /**
         * 重试延迟时间
         */
        private long delayTime = 60L;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MsgHandlerPool {
       private int maxIntervalTime = 10 * 60;

       private int maxDiscardCount = 20;
    }

}
